﻿/*******************************************************
 *  就业模块 相关操作js
 * <p>Title: jobSeeManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年8月22日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//发布用工需求
var index = 1,//显示第几页
    sizes = 1,//每页记录数
    searchOptions;//查询参数
var jobSeeManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        jobSeeManager.initWindowHeight();
        jobSeeManager.initSelect();
        jobSeeManager.initDataList(true);
        jobSeeManager.bindEvent();
        parent.delLoadBoxs();
    },
    //初始化页面高度参数
    initWindowHeight:function(){
        var temp = $(window).height() - 50 - 120;
        sizes = parseInt(temp / 122);//动态计算每页显示记录数
    },
    //初始化人员列表
    initDataList: function (bindPage) {        
        $.ajax({
            type: 'post',
            url: ctx + "/BaneJobIssue/InitJobTable",
            dataType: 'json',
            async: false,
            data:  (bindPage) ? jobSeeManager.getSearchData() : searchOptions,
            success: function (data) {
                if (data){
                    jobSeeManager.bindPersonData(data.rows);//绑定数据
                    if (bindPage) {
                        var page = 0;
                        if (data.total < sizes)
                            page = 1;
                        else {
                            if (data.total % sizes > 0)
                                page = parseInt(data.total / sizes) + 1;
                            else
                                page = parseInt(data.total / sizes);
                        }                            
                        jobSeeManager.bindPagin(page);//绑定分页
                    }         
                }
            }
        });
        parent.delLoadBoxs();
    },
    //绑定招聘信息数据
    bindPersonData: function (result) {
        $('#job_content').empty();
        $.each(result,function(index,item){
            var startDate = new Date(parseInt(item.issue_start.substring(6,item.issue_start.length-2))), endDate = new Date();
            var issue_style = (index===0)?"no-top-border":"",//是否为第一条
                issue_time = "",//发布小时：分钟
                issue_day = "";//什么时候发布
            if (item.issue_start_date === $.formatDate(endDate)) {
                issue_time = item.issue_time;
                var tempValue = $.getIntervalHour(startDate, endDate);
                if (!tempValue || tempValue === 0)
                    issue_day = $.getIntervalMinute(startDate, endDate) + " 分钟前";
                else
                    issue_day = $.getIntervalHour(startDate, endDate) + " 小时前";
            } else
                issue_day = item.issue_start_date;
            //工资金额处理
            var wageNum = "面议";
            if (item.use_wage_start > 0 && item.use_wage_end === 0)
                wageNum = item.use_wage_start + "以上";
            if (item.use_wage_end > 0)
                wageNum = item.use_wage_start + "-" + item.use_wage_end;
            //福利处理
            var use_welfare = (item.use_welfare) ? item.use_welfare.split(',') : "",
                use_welfare_span = "";
            $.each(use_welfare, function (i, n) {
                use_welfare_span += "<span>" + n + "</span>";
            });
            //标题处理
            var nameTitle = item.job_title;
            if (nameTitle.length > 25)
                nameTitle = item.job_title.substring(0, 25) + "...";
            var strHtml = "<div class='timeline-item'><div class='row'>" +
                                "<div class='col-xs-3 date'><i class='fa fa-briefcase'></i> " + issue_time +
                                    "<br><small class='text-navy'>" + issue_day + "</small></div>" +
                                "<div class='col-xs-9 content wageContent " + issue_style + "'><div class='col-xs-5'>" +
                                        "<p class='m-b-xs'><strong>" + nameTitle + "</strong></p>" +
                                        "<p class='wageTitle'>" + wageNum + "<label class='wageMonth'>&nbsp;元/月</label></p>" +
                                        "<p class='wagewelfare'>" + use_welfare_span + "</p></div><div class='col-xs-4 wageCompany'><div><label>" + item.com_name + "</label></div><p class='wageAsk'>" +
                                            "<span>" + item.work_type + "&nbsp;&nbsp;|&nbsp;&nbsp;</span>" +
                                            "<span>" + ((item.use_edu) ? item.use_edu : "不限") + "&nbsp;&nbsp;|&nbsp;&nbsp;</span>" +
                                            "<span>" + ((item.use_ageLimit) ? item.use_ageLimit : "不限") + "</span></p></div>" +
                                    "<div class='col-xs-3' style='text-align:center;'><button type='button' style='margin-top:30px;' onclick='jobSeeManager.openJobDetail("+item.job_id+");' class='btn btn-w-m btn-warning'>详情</button></div></div></div></div>";
            $(strHtml).appendTo($('#job_content'));
        }); 
    },
    //获取查询数据
    getSearchData: function () {
        //判断起薪
        var wageStart = $('#wageStart').val();
        if (wageStart) {
            var result = wageStart.match(/^[0-9]*$/);
            if (!result){
                layer.msg('温馨提示：起薪必须录入数字型数据！',{icon:5});
                return null;
            }
        }else
            wageStart=0;
        //判断薪资截止
        var wageEnd = $('#wageEnd').val();
        if (wageEnd) {
            var result = wageEnd.match(/^[0-9]*$/);
            if (!result){
                layer.msg('温馨提示：薪资截止必须录入数字型数据！',{icon:5});
                return null;
            }
        }else
            wageEnd=0;
        searchOptions = {
            page: index,
            rows: sizes,
            use_status:2,//已发布
            workType: $('#workType').val(),
            workCity: $('#workCity').val(),
            wageStart: wageStart,
            wageEnd: wageEnd,
            userSex: $('#userSex').val(),
            userEdu: $('#userEdu').val(),
            userAge: $('#userAge').val(),
            issueStart: $('#issueStart').val(),
            useAgeLimit: $('#useAgeLimit').val()
        }
        return searchOptions;
    },
    //绑定分页控件
    bindPagin: function (recordCount) {
        $('#pagination').bootstrapPaginator({
            currentPage: index,//当前页
            totalPages: recordCount,//总页数 总数/每页数量
            numberOfPages: 10,//设置控件显示的页码数
            bootstrapMajorVersion: 3,//如果是bootstrap3版本需要加此标识，并且设置包含分页内容的DOM元素为UL,如果是bootstrap2版本，则DOM包含元素是DIV
            useBootstrapTooltip: true,//是否显示tip提示框
            itemTexts: function (type, page, current) {//文字翻译
                switch (type) {
                    case "first":
                        return "首页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "尾页";
                    case "page":
                        return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) {
                //给每个页眉绑定一个事件，其实就是ajax请求
                index = page;
                initDataList();
            }
        });
    },
    //初始化下拉
    initSelect: function () {
        $('#workType').initSelectpicker("JobPost",null,true);//岗位
        //$('#workCity').initSelectpicker("SystemArea", null, true);//工作城市
        $('#userEdu').initSelectpicker("JobEdu", null, true);//学历
        $('#userAge').initSelectpicker("JobWorkAge", null, true);//年龄
        $('#useAgeLimit').initSelectpicker("WorkExperience", null, true);//工作经验
        $('.selectpicker').selectpicker({ size: 6 });
    },
    //绑定默认事件
    bindEvent: function () {
        //查询
        $('#btnSearch').click(function () {
            parent.showLoadBoxs();
            //重新查询需要 重置分页控件
            index = 1;
            jobSeeManager.initDataList(true);
        });
    },
    //打开招聘详细
    openJobDetail: function (job_id) {
        if (!job_id) {
            layer.msg("参数异常~", { icon: 5 });
            return false;
        }
        //修改浏览数量
        $.ajax({
            type: 'post',
            url: ctx + "/BaneJobIssue/modifyBrowseJobNum/" + job_id,
            dataType: 'json',
            async: false,
            success: function (data) {

            }
        });
        //获取数据
        $.ajax({
            type: 'post',
            url: ctx + "/BaneJobIssue/GetJobDetailById/" + job_id,
            dataType: 'json',
            async: false,
            success: function (data) {
                if (data.Statu != 0) {
                    layer.msg("数据为空！");
                    return false;
                }
                var item = data.Data;
                layer.open({
                    title: ['招聘详情', 'font-size:18px;'],
                    type: 1,
                    content: $('#org_form'),
                    scroll: true,//是否显示滚动条、默认不显示
                    btn: '',
                    area: ['850px', '95%'],
                    success: function (li, o) {
                        parent.showLoadBoxs();
                        var startDate = new Date(parseInt(item.issue_start.substring(6, item.issue_start.length - 2))), endDate = new Date();
                        var $startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()),
                            $endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate());
                        var day = $.getIntervalDay($startDate, $endDate);
                        $('#iseeDay').text(day);//几天前
                        $('#browseNum').text(item.browse_num);//浏览次数
                        $('#iseeDate').text($.formatDate(startDate));//发布时间
                        $('#jobWorkType').text(item.work_type);//岗位
                        $('#jobTitle').text(item.job_title);//标题
                        var str = (item.use_welfare) ? item.use_welfare.split(',') : null;
                        $('#jobWelfare').empty();
                        if (str) {
                            var strHtml = "";
                            $.each(str, function (index,n) {
                                strHtml += "<span>" + n + "</span>";
                            });
                            $(strHtml).appendTo($('#jobWelfare'));
                        }
                        //工资
                        if (item.use_wage_start > 0 && item.use_wage_end === 0) {
                            $('#jobWageNum').text(item.use_wage_start);//工资
                            $('#jobMonth').text("以上");
                        }
                        if (item.use_wage_start === 0 && item.use_wage_end === 0) {
                            $('#jobWageNum').text("面议");//工资
                            $('#jobMonth').text("");
                        }
                        if (item.use_wage_end > 0) {
                            $('#jobWageNum').text(item.use_wage_start + "-" + item.use_wage_end);//工资
                            $('#jobMonth').text("元/月");
                        }
                        $('#jobCount').text((!item.use_num ? "不限" : item.use_num));//招聘几人
                        $('#jobEdu').text((!item.use_edu ? "不限" : item.use_edu));//学历
                        $('#jobLimit').text((!item.use_ageLimit ? "不限" : item.use_ageLimit));//经验
                        $('#jobPhone').text((!item.use_phone ? "" : item.use_phone));//电话
                        $('#jobEmail').text((!item.use_email ? "" : item.use_email));//邮箱
                        $('#jobAddr').text((!item.addr ? "" : item.addr));//地址
                        $('#jobPostSaid').text((!item.postNote ? "" : item.postNote));//职位描述
                        $('#jobComSaid').text((!item.use_note ? "" : item.use_note));//公司简介
                        parent.delLoadBoxs();
                    }
                });
            }
        });
    }
}

